<?php
namespace lib\db;
class MySqlj
{
    private $conn;
    public function __construct($user,$pwd,$ip,$db,$port=3306,$charset="UTF8")
    {
        $this->conn=mysqli_connect($ip,$user,$pwd,$db,$port);
        if ($this->conn){
            mysqli_query($this->conn,"SET NAMES $charset");
        }else{
            $this->conn=false;
        }
    }
    public function IsConnect()
    {
        return $this->conn?true:false;
    }
    public function get($sql)
    {
        $q=mysqli_query($this->conn,$sql);
        $arr=array();
        while ($data=mysqli_fetch_assoc($q))
        {
           array_push($arr,$data);
            // $arr[]=$data;
        }
        mysqli_free_result($q);
        return $arr;
    }
    public function insert($tb_name,$arr)
    {
        $arr_key=array_keys($arr);
        $arr_values=array_values($arr);
        $str_field_name=implode("`,`",$arr_key);
        $str_value=implode("','",$arr_values);
        $sql="INSERT INTO $tb_name (`$str_field_name`) VALUES ('$str_value')";
      //  echo $sql;
       //exit;
        return  mysqli_query($this->conn,$sql);
    }
    public function shan($a,$b)
    {
        $arr_key=array_keys($b);
        $arr_key1=array_values($b);
        $arr_ku=implode($arr_key);
        $arr_ko=implode($arr_key1);
        $sql="DELETE FROM $a WHERE $arr_ku=$arr_ko";

        mysqli_query($this->conn,$sql);
    }
    public function gai($a,$b,$c)
    {
        $arr1=array_keys($b);
        $arr2=array_values($b);
        $arr3=array();
        for ($i=0;$i<count($arr1);$i++){
            $arr3[$i]=$arr1[$i]."='".$arr2[$i]."',";
        }
        $aur1 =implode('',$arr3);
        $aur1=substr($aur1,0,strlen($aur1)-1);
        $sql="UPDATE $a SET $aur1 WHERE $c";
        //echo $sql;
       // exit;
        mysqli_query($this->conn,$sql);

    }
    public function lk()
    {
        return $this->conn;
    }
    public function query($sql)
    {
        return mysqli_query($this->conn,$sql);
    }
    public function __destruct()
    {
        mysqli_close($this->conn);
    }
}


//$mysql=new MySqlj("root","e7a9e5b197","127.0.0.1","teacher");